# -*- coding: utf-8 -*-
from abc import ABC

from app.views import CommonHandler
from app.models.dao import db2vectors, delete, add
from app.models.model import SMPoweredAreaModel
import app.tools.SafeMonitorTools.api as api
import uuid

class Powered_Area_Handler(CommonHandler, ABC):
    async def get(self):
        meta_id = self.get_argument("meta_id", None)
        mine_id = self.get_argument("mine_id", None)
        layer = self.get_argument("layer", None)
        # 获取 断电区域位置信息
        areasPosion = db2vectors(SMPoweredAreaModel, meta_id)
        # 获取 断电区域 关联信息
        areasInfo = await api.get_regions(mine_id,layer)
        await self.respose(data={'areasPosition':areasPosion,'areasInfo':areasInfo})

    async def post(self):
        params = self.json_params()
        for points in params['polygons']:
            AreaId = str(uuid.uuid4())
            flag = await api.add_regions(params['mine_id'],AreaId,params['layer'])
            responCode = ''
            # 判断 flag 确定 kj83上 是否添加成功
            if flag:
                Area = SMPoweredAreaModel({'mine_id':params['mine_id'],'meta_id':params['meta_id'],'id':AreaId,'points':str(points)})
                add(Area)
                responCode = 'success'
            else:
                responCode = 'error'
        await self.respose(data={'code': responCode})

    async def put(self):
        params = self.json_params()
        areaId = params['id']
        node = params['nodeid']
        mine_id = params['mine_id']
        flag = await api.put_regions(mine_id,areaId,node)
        await self.respose(data={'flag': flag})

    async def delete(self):
        params = self.json_params()
        mine_id = params['mine_id']
        id = params['id']
        flag = await api.delete_regions(mine_id,id)
        if flag:
            delete(SMPoweredAreaModel,id)
        await self.respose(data={'flag': flag})